package com.banter;

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindBanter extends Activity implements OnClickListener, OnLongClickListener {
	
	private Button m_FindButton;
	private EditText m_SearchedTitle;
	private TextView m_Location;
	private TextView m_Results;
	private String searchTitle;
	
	//fake database
	private String example1 = "Dr.D Sucks";
	private String example2 = "Towson";
	private String example3 = "7800 york";
	private String example4 = "Baltimore";
	private String example5 = "Pizza";
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_banter);
		initLayout();
		m_FindButton.setOnClickListener(this);
		m_Results.setOnLongClickListener(this);
		
		BanterLocationListener gps = new BanterLocationListener(FindBanter.this);
		
		if(!gps.canGetLocation()){
			gps.showSettingsAlert();
			m_Location.setText("Location: Unavailable");
		}else{
			double lattitude = gps.getLat();
			double longitude = gps.getLong();
			
			m_Location.setText("Location: ( "+lattitude+", "+longitude+" )");
		}
		
	}
	protected void initLayout(){
    	m_FindButton = (Button) this.findViewById(R.id.button1);
    	m_SearchedTitle = (EditText) this.findViewById(R.id.editText1);
    	m_Results = (TextView) this.findViewById(R.id.textView2);
    	m_Location = (TextView) this.findViewById(R.id.locationText);
    	
	}
	
	@Override
	public void onClick(View v) {
		m_Results.setText("");
		searchTitle = m_SearchedTitle.getText().toString();
		checkDataBase();
		m_SearchedTitle.setText("");
		
	}
	public boolean onLongClick(View v){
		TextView search_Result = (TextView) v;
		String title = search_Result.getText().toString();
		String body = " ";
		Intent myIntent = new Intent(FindBanter.this, ChatActivity.class);
		myIntent.putExtra("title",title);
		myIntent.putExtra("body", body);
		startActivity(myIntent);
		return false;
		
	}
	
	public void checkDataBase(){
		if (searchTitle.equals(example1)){
			m_Results.setText(searchTitle);
		}
		else if (searchTitle.equals(example2)){
			m_Results.setText(searchTitle);
		}
		else if (searchTitle.equals(example3)){
			m_Results.setText(searchTitle);
		}
		else if (searchTitle.equals(example4)){
			m_Results.setText(searchTitle);
		}
		else if (searchTitle.equals(example5)){
			m_Results.setText(searchTitle);
		}
		else {
			m_Results.setText("No Results Found");
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_find_banter, menu);
		return true;
	}

}
